package net.yla.smartbus.module.ba.dept.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.vip.vjtools.vjkit.collection.ListUtil;
import net.yla.smartbus.core.api.RequestCode;
import net.yla.smartbus.core.base.PageBean;
import net.yla.smartbus.core.exception.AppException;
import net.yla.smartbus.module.ba.dept.dto.DeptDto;
import net.yla.smartbus.module.ba.dept.dto.EmpInfoDTO;
import net.yla.smartbus.module.ba.dept.entity.DeptDO;
import net.yla.smartbus.module.ba.dept.entity.DeptInfoDO;
import net.yla.smartbus.module.ba.dept.entity.GroupChildrenDO;
import net.yla.smartbus.module.ba.dept.form.DeptForm;
import net.yla.smartbus.module.ba.dept.mapper.DeptMapper;
import net.yla.smartbus.module.ba.dept.query.DeptEmpQuery;
import net.yla.smartbus.module.ba.dept.query.DeptQuery;
import net.yla.smartbus.module.ba.dept.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: Yee
 * @date: 2021/6/17 14:44
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    /**
     * 查询部门
     * @param query
     * @return
     */
    @Override
    public PageBean<DeptDto> listDeptInfo(DeptQuery query) {
        if (query.getGroupId() != null) {
            List<Integer> listGroupId = this.listGroupId(query.getGroupId());
            query.setGroupIdList(listGroupId);
        }
        PageHelper.startPage(query.getPageNum(), query.getPageSize(), "deptId DESC");
        List<DeptInfoDO> list = deptMapper.listDeptInfo(query);
        if (ListUtil.isEmpty(list)) {
            return new PageBean<>(ListUtil.emptyList(), query.getPageNum(), query.getPageSize(), 0);
        }
        List<DeptDto> dtoList = new ArrayList<>(list.size());
        int count = deptMapper.countDeptInfo(query);
        for (DeptInfoDO deptInfoDO : list) {
            DeptDto dto = new DeptDto();
            //主键
            dto.setDeptId(deptInfoDO.getDeptId());
            //部门名称
            dto.setDeptName(deptInfoDO.getName());
            //分组id
            if (deptMapper.selectGroupName(deptInfoDO.getGroupId()) != null) {
                dto.setGroupName(deptMapper.selectGroupName(deptInfoDO.getGroupId()));
            } else {
                dto.setGroupName("--");
            }
            dto.setGroupId(deptInfoDO.getGroupId());
            //部门负责人
            if (deptMapper.selectPeoPleName(deptInfoDO.getEmpId()) != null) {
                dto.setPeoPleName(deptMapper.selectPeoPleName(deptInfoDO.getEmpId()));
            } else {
                dto.setPeoPleName("--");
            }
            if (deptInfoDO.getEmpId() != 0) {
                dto.setEmpId(deptInfoDO.getEmpId());
            } else {
                dto.setEmpId(0);
            }
            //办公地址
            dto.setAddress(deptInfoDO.getAddress());
            //办公电话
            dto.setTel(deptInfoDO.getTel());
            //人员数量
            if (deptMapper.countPeople(deptInfoDO.getDeptId()) != null) {
                dto.setCountPeople(deptMapper.countPeople(deptInfoDO.getDeptId()));
            } else {
                dto.setCountPeople(0);
            }
            dto.setPinyin(deptInfoDO.getPinyin());
            dto.setAbbr(deptInfoDO.getAbbr());
            dto.setDisabled(deptInfoDO.getDisabled());
            dto.setFlags(1);
            dtoList.add(dto);
        }
        return new PageBean<>(dtoList, query.getPageNum(), query.getPageSize(), count);
    }

    /**
     * 逻辑删除部门
     * @param query
     * @return
     */
    @Override
    public int deleteDept(DeptQuery query) {
        if (query.getDeptId() == null) {
            throw new AppException("请选择要删除的部门", RequestCode.forbid_error);
        } else {
            int i = deptMapper.deleteDept(query.getDeptId());
            if (i < 0) {
                throw new AppException("删除失败", RequestCode.forbid_error);
            }
        }
        return 1;
    }

    @Override
    public int addDept(DeptForm form) {
        DeptDO record = new DeptDO();
        if (form.getName() == null) {
            throw new AppException("部门名称不能为空", RequestCode.forbid_error);
        }
        if (form.getPinyin() == null) {
            throw new AppException("部门拼音不能为空", RequestCode.forbid_error);
        }
        if (form.getAbbr() == null) {
            throw new AppException("拼音缩写不能为空", RequestCode.forbid_error);
        }
        if (deptMapper.selectDeptName(form.getName()) > 0) {
            throw new AppException("部门名称重复", RequestCode.forbid_error);
        }
        if (form.getTel()!=null){
            if (form.getTel().length()>11){
                throw new AppException("办公电话格式不对", RequestCode.forbid_error);
            }
        }
        record.setName(form.getName());
        record.setAbbr(form.getAbbr());
        record.setAddress(form.getAddress());
        record.setEmpId(form.getEmpId());
        record.setTel(form.getTel());
        record.setDisabled(form.getDisabled());
        record.setPinyin(form.getPinyin());
        record.setQualifiedName(form.getName());
        record.setGroupId(form.getGroupId());
        int i = deptMapper.insertSelective(record);
        if (i<0){
            throw new AppException("新增失败", RequestCode.forbid_error);
        }
        return 1;
    }

    /**
     * 修改部门信息
     * @param form
     * @return
     */
    @Override
    public int update(DeptForm form) {
        DeptDO record = new DeptDO();
        if (form.getName() == null) {
            throw new AppException("部门名称不能为空", RequestCode.forbid_error);
        }
        if (form.getPinyin() == null) {
            throw new AppException("部门拼音不能为空", RequestCode.forbid_error);
        }
        if (form.getAbbr() == null) {
            throw new AppException("部门首字母不能为空", RequestCode.forbid_error);
        }
        if (form.getTel()!=null){
            if (form.getTel().length()>11){
                throw new AppException("办公电话格式不对", RequestCode.forbid_error);
            }
        }
        System.out.println(form.getTel().length());
        record.setLastModifyTime(new Date());
        record.setDeptId(form.getDeptId());
        record.setName(form.getName());
        record.setAbbr(form.getAbbr());
        record.setAddress(form.getAddress());
        record.setEmpId(form.getEmpId());
        record.setTel(form.getTel());
        record.setDisabled(form.getDisabled());
        record.setPinyin(form.getPinyin());
        record.setQualifiedName(form.getName());
        record.setGroupId(form.getGroupId());

        if (form.getDeptId() != null){
            record.setVersion(deptMapper.selectVersion(form.getDeptId()) + 1);
            int i = deptMapper.updateByPrimaryKeySelective(record);
            List<Integer> list = deptMapper.listPeopleId();
            for (Integer id : list) {
                int updatePeoples = deptMapper.updatePeoples(form.getDeptId(), id);
            }
            if (form.getEmpIds() != null && form.getEmpIds().size() > 0) {
                for (Integer empId : form.getEmpIds()) {
                    //int updatePeoples = departmentManageDOMapper.updatePeoples(form.getDeptId(), empId);
                    int updatePeople = deptMapper.updatePeople(form.getDeptId(), empId);
                }
            }
        }else {
            Integer deptName = deptMapper.selectDeptName(form.getName());
            if (deptName>0){
                throw new AppException("部门名称不符合规定",RequestCode.forbid_error);
            }
            int i = deptMapper.insertSelective(record);
            if (i<0){
                throw new AppException("新增失败", RequestCode.forbid_error);
            }
        }

        return 1;
    }

    /**
     * 查询子分组id
     *
     * @param parentId
     * @return
     */

    public List<Integer> listGroupId(Integer parentId) {
        List<GroupChildrenDO> list = deptMapper.listGroupId(parentId);
        List<Integer> integers = new ArrayList<>(list.size());
        integers.add(parentId);
        for (GroupChildrenDO groupChildrenDO : list) {
            integers.add(groupChildrenDO.getGroupId());
        }
        return integers;
    }

    /**
     * 查询非本部门人员
     *
     * @param query
     * @return
     */
    @Override
    public List<EmpInfoDTO> listEmpInfo(DeptEmpQuery query) {
        if (query.getGroupId() != null) {
            if (query.getGroupId() == 0) {
                Integer i = null;
                query.setGroupId(i);
            }
        }
        List<EmpInfoDTO> empInfoDTOS = deptMapper.listEmpInfo(query);
        return empInfoDTOS;
    }

    /**
     * 查询是本部门人员
     *
     * @param deptId
     * @return
     */
    @Override
    public List<Integer> listHavePeople(Integer deptId) {
        return deptMapper.listHavePeople(deptId);
    }

    /**
     * 部门下拉框
     * @param token
     * @return
     */
    @Override
    public List<JSONObject> departments(String token) {
        // TODO: common
        return deptMapper.departments();
    }

}
